<template>
  <div class='page-address-list'>
    <div class="top-of-list">
      <div class="have-default-address" v-if="showDefaultFlag">
        <img class="default-address-image" src="../../assets/defaultAddress.png"/>
        <div class="default-address-font">
          <div class="customer-info">
            <span class="customer-name">{{defaultAddress.receivingGoodsName}}</span>
            <span class="customer-phone">{{defaultAddress.tel}}</span>
          </div>
          <div class="customer-address">
            {{defaultAddress.provinceCityAreaName}}{{defaultAddress.addressDetail}}
          </div>
        </div>
      </div>
      <div @click="newAddress">
        <div class="save-new-address" v-if="!showDefaultFlag">
          <img class="save-new-address-img" src="../../assets/jifen-img.png"/>
          <span class="save-new-address-font">添加新地址</span>
        </div>
      </div>
    </div>
    <div class="address-list">
      <div v-for="(item,index) in addressList" class="address-item" :key="index">
        <img class="address-item-image" src="../../assets/defaultAddress.png" v-if="item.isDefault"/>
        <img class="address-item-image" src="../../assets/notDefaultAddress.png" v-if="!item.isDefault"/>
        <div class="address-item-font">
          <div class="customer-info">
            <span class="customer-name">{{item.receivingGoodsName}}</span>
            <span class="customer-phone">{{item.tel}}</span>
            <span class="tip-default" v-if="item.isDefault === 1">默认</span>
          </div>
          <div class="customer-address">
            {{item.provinceCityAreaName}}{{item.addressDetail}}
          </div>
        </div>
        <div @click="updateAddress(item.id)" class="address-item-edit">
          编辑
        </div>
      </div>
    </div>
    <router-link to="/user/editAddress">
      <img class="add-address-image" src="../../assets/addIcon.png"/>
    </router-link>
  </div>
</template>
<script>
import { API } from '../../config'
export default {
  name: 'AddressList',
  data() {
    return {
      addressList: [],
      defaultAddress: {},
      showDefaultFlag: false,
      ids: '',
      productSkuId: ''
    }
  },
  methods: {
    newAddress () {
      let vm = this
      vm.$router.push('/user/editAddress');
    },
    updateAddress (id) {
      let vm = this
      vm.$router.push('/user/editAddress?id=' + id);
    },
    getList() {
      let vm = this
      let accesstoken = vm.getLocalStorage('accesstoken')
      vm.$axios.get(API + '/receivinggoodsaddress/getList',{
          headers: {'accesstoken': accesstoken}//设置header信息
        }).then((response) => {
        if (response.data.code === 200) {
            vm.addressList = response.data.data
        } 
        if (vm.addressList.length === 0) {
          vm.defaultAddress = {}
        } else {
          vm.defaultAddress = vm.addressList.filter(item => item.isDefault)
          vm.addressList.filter(item => {
            if (item.isDefault === 1) {
              vm.defaultAddress = item
              vm.showDefaultFlag = true
            }
          })
        }
      })
    }
  },
  mounted() {
    let vm = this;
    vm.getList();
  }
}
</script>

<style lang='less' scoped>
@import '../../css/common.less';
@import '../../css/addressList.less';
</style>
